﻿using Microsoft.Extensions.Logging;

namespace LoggerTest.CustomLogger
{
    internal class CustomLogger(string categoryName, CustomLoggerOptions options) : ILogger
    {
        internal CustomLoggerOptions Options { get; set; } = options;
        private readonly IExternalScopeProvider scopeProvider = new LoggerExternalScopeProvider();
        private readonly string _categoryName = categoryName;
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
        {
            if (!IsEnabled(logLevel)) return;
            Console.ForegroundColor = Options.Color;
            Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{logLevel}] [{_categoryName}] {formatter(state, exception)}");
            Console.ForegroundColor = ConsoleColor.Gray;
        }
        public IDisposable? BeginScope<TState>(TState state) where TState : notnull => scopeProvider.Push(state);
        public bool IsEnabled(LogLevel logLevel) => logLevel >= Options.MinLevel && logLevel <= Options.MaxLevel;
    }
}
